﻿using DotNetty.Transport.Channels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace SpecialDelimiterServer
{
    public class DelimiterServerHandler : SimpleChannelInboundHandler<string>
    {

        public override void ChannelActive(IChannelHandlerContext context)
        {
            // 这里用了$_做分隔符
            context.WriteAndFlushAsync(string.Format("welcome to {0} !$_", Dns.GetHostName()));
            context.WriteAndFlushAsync(string.Format("it is {0} now !$_", DateTime.Now));
            string resp = "ejfgodk$_ejfgodk$_ejfgodk$_";
            context.WriteAndFlushAsync(resp);
        }

        public override void ChannelReadComplete(IChannelHandlerContext context)
        {
            // 接收完数据清空缓冲区
            context.Flush();
        }

        public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
        {
            Console.WriteLine(exception.Message);
            // 发生异常,关闭通道
            context.DisconnectAsync();
            context.CloseAsync();
        }

        protected override void ChannelRead0(IChannelHandlerContext ctx, string msg)
        {
            string response= "ejfgodk$_ejfgodk$_ejfgodk$_";
            ctx.WriteAndFlushAsync(response);
        }
    }
}
